package id.co.shuba.spamobile.util;

import id.co.shuba.spamobile.App;
import id.co.shuba.spamobile.IDatePickerTarget;
import id.co.shuba.spamobile.task.LoginTask;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.text.format.DateFormat;
import android.text.format.Time;
import android.view.ContextThemeWrapper;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

public class DialogUtil {
	public static final String DATE_FORMAT = "yyyy-MM-dd";
	
	private static SimpleDateFormat sdf;
	private static SimpleDateFormat dateTimeFormat;
	static {
		sdf = (SimpleDateFormat)java.text.DateFormat.getDateInstance();
		sdf.applyPattern(DATE_FORMAT);
	    sdf.setLenient(true);
	    dateTimeFormat = (SimpleDateFormat)java.text.DateFormat.getDateInstance();
	    dateTimeFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
	    dateTimeFormat.setLenient(true);	    
	}
	
	public static String formatDateOnly(Date date) {
		return sdf.format(date);
	}
	
	public static boolean isValid(String date) {	
		try { sdf.parse(date); return true; }
		catch (Exception e) { return false; }				
	}
	
	public static boolean isValidDateTime(String date) {	
		try { dateTimeFormat.parse(date); return true; }
		catch (Exception e) { return false; }				
	}
	
	public static boolean isValidAfterward(String date) {
		try {
			Calendar c1 = GregorianCalendar.getInstance();
			c1.setTime(sdf.parse(date));
			Calendar c2 = GregorianCalendar.getInstance();
			c2.setTime(dateTimeFormat.parse(App.getProcessDateTime()));
						
			return dateCompare(c1, c2) >= 0;
		}
		catch (Exception e) { return false; }
	}
	
	public static boolean isValidAfterward2(String date) {
		try {			
			Calendar c1 = GregorianCalendar.getInstance();
			c1.setTime(dateTimeFormat.parse(date));
			Calendar c2 = GregorianCalendar.getInstance();
			c2.setTime(dateTimeFormat.parse(App.getProcessDateTime()));
						
			return dateTimeCompare(c1, c2) >= 0;
		}
		catch (Exception e) { return false; }
	}
	
	public static boolean isValidTime(String time) {
		try {
			String[] split = time.split(":");
			int h = Integer.parseInt(split[0]);
			int m = Integer.parseInt(split[1]);
			if (m == 0 && h == 24)
				return true;
			
			return (h < 24 && m < 60);
		} catch (Exception e) {
			return false;
		}
	}
	
	public static boolean isValidAfterward(String date, String date2) {
		try {
			Calendar c1 = GregorianCalendar.getInstance();
			c1.setTime(sdf.parse(date));
			Calendar c2 = GregorianCalendar.getInstance();
			c2.setTime(sdf.parse(date2));
						
			return dateCompare(c1, c2) >= 0;
		}
		catch (Exception e) { return false; }
	}
	
	public static boolean isValidAfterward2(String date, String date2) {
		try {			
			Calendar c1 = GregorianCalendar.getInstance();
			c1.setTime(dateTimeFormat.parse(date));
			Calendar c2 = GregorianCalendar.getInstance();
			c2.setTime(dateTimeFormat.parse(date2));
						
			return dateTimeCompare(c1, c2) >= 0;
		}
		catch (Exception e) { return false; }
	}
	
	public static int dateCompare(Calendar c1, Calendar c2) {
		if (c1.get(Calendar.YEAR) != c2.get(Calendar.YEAR)) 
	        return c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
	    if (c1.get(Calendar.MONTH) != c2.get(Calendar.MONTH)) 
	        return c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
	    return c1.get(Calendar.DAY_OF_MONTH) - c2.get(Calendar.DAY_OF_MONTH);
	}	
	
	public static int dateTimeCompare(Calendar c1, Calendar c2) {
		if (c1.get(Calendar.YEAR) != c2.get(Calendar.YEAR)) 
	        return c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
	    if (c1.get(Calendar.MONTH) != c2.get(Calendar.MONTH)) 
	        return c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
	    if (c1.get(Calendar.DAY_OF_MONTH) != c2.get(Calendar.DAY_OF_MONTH)) 
	        return c1.get(Calendar.DAY_OF_MONTH) - c2.get(Calendar.DAY_OF_MONTH);
	    if (c1.get(Calendar.HOUR_OF_DAY) != c2.get(Calendar.HOUR_OF_DAY)) 
	        return c1.get(Calendar.HOUR_OF_DAY) - c2.get(Calendar.HOUR_OF_DAY);
	    if (c1.get(Calendar.MINUTE) != c2.get(Calendar.MINUTE)) 
	        return c1.get(Calendar.MINUTE) - c2.get(Calendar.MINUTE);	   
	    return c1.get(Calendar.SECOND) - c2.get(Calendar.SECOND);
	}	
	
	public static String changeFormat(String date) {
		if (Str.isNullOrEmpty(date))
			return date;
		
		try {
			String[] split = date.split("-");
			return String.format("%s-%s-%s", split[2], split[1], split[0]);
		} catch (Exception e) {
			return date;
		}
	}
	
	public static Dialog create(Activity activity, final EditText target) {		
		String date = target.getText().toString();
		
		Calendar cal = Calendar.getInstance();
		int day = cal.get(Calendar.DAY_OF_MONTH);
		int month = cal.get(Calendar.MONTH);
		int year = cal.get(Calendar.YEAR);
		
		if (date != null && !date.trim().equals("")) {
			try {
				String[] values = date.split("-");
				year = Integer.parseInt(values[2]);
				month = Integer.parseInt(values[1]) -1;
				day = Integer.parseInt(values[0]);
			} catch (Exception e) {					
			}				
		}
		DatePickerDialog dateDlg = new DatePickerDialog(new ContextThemeWrapper(activity,
				id.co.shuba.spamobile.R.style.Theme_Sherlock_Light_Dialog),
				new DatePickerDialog.OnDateSetListener() {
					public void onDateSet(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
						Time chosenDate = new Time();
						chosenDate.set(dayOfMonth, monthOfYear, year);
						long dtDob = chosenDate.toMillis(true);
						CharSequence strDate = DateFormat.format(
								"dd-MM-yyyy", dtDob);
						target.setText(strDate);
					}
				}, year, month, day);		
		dateDlg.setMessage("Select Date");		

		return dateDlg;
	}
	
	public static Dialog createTimePicker(Activity activity, final EditText target) {		
		String time = target.getText().toString();
		
		Calendar cal = Calendar.getInstance();
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		int minute = cal.get(Calendar.MINUTE);		
		
		if (time != null && !time.trim().equals("")) {
			try {
				String[] values = time.split(":");
				hour = Integer.parseInt(values[0]);
				minute = Integer.parseInt(values[1]);				
			} catch (Exception e) {					
			}				
		}
		TimePickerDialog dateDlg = new TimePickerDialog(new ContextThemeWrapper(activity,
				id.co.shuba.spamobile.R.style.Theme_Sherlock_Light_Dialog),
				new OnTimeSetListener() {			
			@Override
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {				
				target.setText(toTimeString(hourOfDay, minute));
				
			}
		}, hour, minute, true);

		dateDlg.setMessage("Select Time");

		return dateDlg;
	}
	
	public static String toTimeString(int hour, int minute) {
		String h = String.valueOf(hour);
		String m = String.valueOf(minute);
		if (h.length()==1)h="0"+h;
		if (m.length()==1)m="0"+m;
		return h+":"+m;
	}	
	
	public static void showMessage(Context ctx, String title, String message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(ctx,
				id.co.shuba.spamobile.R.style.Theme_Sherlock_Light_Dialog));
		        
        builder.setTitle(title);
        builder.setMessage(message);                      
        builder.show();
	}	
}
